package com.zhangzy.qm.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/** 
 * @Class Name FileUtil
 * @Author zhenyu
 * @Create In 2012-3-27
 */
public class FileUtil {

	/**
	 * 删除某个文件夹下的所有文件夹和文件 
	 * @param delpath String
	 * @Create In 2012-4-4 By zhenyu
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @return boolean
	 */
	public static boolean deletefile(String delpath)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(delpath);
			if (!file.isDirectory()) {
				file.delete();
			} else if (file.isDirectory()) {
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File delfile = new File(delpath + "/" + filelist[i]);
					if (!delfile.isDirectory()) {
						delfile.delete();
						System.out.println("删除文件成功");
					} else if (delfile.isDirectory()) {
						deletefile(delpath + "/" + filelist[i]);
					}
				}
				file.delete();
			}
		} catch (FileNotFoundException e) {
			System.out.println("deletefile() Exception:" + e.getMessage());
		}
		return true;
	}

	/**
	 * 读取某个文件夹下的所有文件夹的文件, 返回所有文件名 
	 * @param filepath  String
	 * @Create In 2012-4-4 By zhenyu 
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @return Map<Integer, String> pathMap 
	 */
	public static Map<Integer, String> readFileName(String filepath,
			Map<Integer, String> pathMap) throws Exception {
		if (pathMap == null) {
			pathMap = new HashMap<Integer, String>();
		}
		File file = new File(filepath);
		// 文件
		if (!file.isDirectory()) {
			pathMap.put(pathMap.size(), file.getPath());
		} else if (file.isDirectory()) { // 如果是目录， 遍历所有子目录取出所有文件名
			String[] filelist = file.list();
			for (int i = 0; i < filelist.length; i++) {
				File readfile = new File(filepath + "/" + filelist[i]);
				if (!readfile.isDirectory()) {
					pathMap.put(pathMap.size(), readfile.getPath());
				} else if (readfile.isDirectory()) { // 子目录的目录
					readFileName(filepath + "/" + filelist[i], pathMap);
				}
			}
		}
		return pathMap;
	}

	/**
	 * 读取某个文件夹下的所有文件夹的文件, 返回所有文件名
	 * @Methods Name readFileName
	 * @Create In 2012-4-4 By zhenyu
	 * @param filepath
	 * @return
	 * @throws Exception List<String>
	 */
	public static ArrayList<String> readFileName(String filepath,
			ArrayList<String> fileList) throws Exception {
		if (filepath != null) {
			File file = new File(filepath);
			if (!file.isDirectory()) {// 文件
				fileList.add(file.getPath());
			} else if (file.isDirectory()) { // 如果是目录， 遍历所有子目录取出所有文件名
				String[] filelist = file.list();
				for (int i = 0; i < filelist.length; i++) {
					File readfile = new File(filepath + "/" + filelist[i]);
					if (!readfile.isDirectory()) {
						fileList.add(readfile.getPath());
					} else if (readfile.isDirectory()) { // 子目录的目录
						readFileName(filepath + "/" + filelist[i], fileList);
					}
				}
			}
		}
		return fileList;
	}

	/**
	 * 读文件
	 * @Methods Name readFile
	 * @Create In 2012-4-4 By zhenyu
	 * @param filePath
	 * @return
	 * @throws Exception String
	 */
	public static String readFile(String filePath) throws Exception {
		StringBuilder sb = new StringBuilder();
		String data = "";
		BufferedReader sr =null;
		try{
			sr = new BufferedReader(new InputStreamReader(
					new FileInputStream(filePath),"utf-8"));
			while ((data = sr.readLine()) != null) {
				sb.append(data);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(sr!=null){
				sr.close();
			}			
		}			
		return sb.toString();
	}

	/**
	 * 读文件
	 * @Methods Name readFile2List
	 * @Create In 2012-4-4 By zhenyu
	 * @param filePath
	 * @return
	 * @throws Exception ArrayList<String>
	 */
	public static ArrayList<String> readFile4List(String filePath)
			throws Exception {
		ArrayList<String> list = new ArrayList<String>();
		String data = "";
		BufferedReader sr = new BufferedReader(new InputStreamReader(
				new FileInputStream(filePath)));
		while ((data = sr.readLine()) != null) {
			list.add(data);
		}
		sr.close();
		return list;
	}

	/**
	 * 写文件
	 * @Methods Name writeFile
	 * @Create In 2012-4-4 By zhenyu
	 * @param list
	 * @param filePath
	 * @throws Exception void
	 */
	public static void writeFile(ArrayList<String> list, String filePath)
			throws Exception {
		BufferedWriter bw = null;
		try{
			bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(filePath)));
			if (list != null && list.size() > 0) {
				int i = 0;
				for (String content : list) {
					bw.write(content);
					if (i != (list.size() - 1)) {
						bw.write("\n");
					}
					i++;
				}
				bw.flush();
			}
		}
		finally {
            if (bw != null) {
                bw.close();
            }
        }
	}

	/**
	 * 写文件
	 * @Methods Name writeFile
	 * @Create In 2012-4-4 By zhenyu
	 * @param content
	 * @param filePath
	 * @throws Exception void
	 */
	public static void writeFile(String content, String filePath)
			throws Exception {
		BufferedWriter bw = null;
		try{
			bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(filePath),"utf-8"));
			bw.write(content);
			bw.flush();
		}finally {
            if (bw != null) {
                bw.close();
            }
        }
	}

	public static void copy(String oldPath, String newPath) throws IOException {
		InputStream inStream = null;
		FileOutputStream fs = null;
		try {
			File oldfile = new File(oldPath);
			int bytesum = 0;
			int byteread = 0;
			if (oldfile.exists()) {
				inStream = new FileInputStream(oldfile);
				fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					fs.write(buffer, 0, byteread);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(inStream!=null){
				inStream.close();
			}
			if(fs!=null){
				fs.close();
			}			
		}
	}
	
	/**
	 * 文件下载
	 * @Methods Name downLoad
	 * @Create In May 7, 2012 By zhenyu
	 * @param fileName
	 * @param path
	 * @param response
	 * @throws IOException void
	 */
	public static void downLoad(String fileName,String path, HttpServletResponse response) throws IOException {
		   String strZipName = fileName + ".zip";
		   BufferedInputStream bis = null;
		   BufferedOutputStream bos = null;
		   OutputStream os = null;
		   InputStream is = null;
		   try {
		    File file = new File(path + strZipName);
		    if (!file.exists()) {
		     System.out.println("文件不存在");
		    }

		    is = new FileInputStream(file);
		    bis = new BufferedInputStream(is);
		    os = response.getOutputStream();
		    bos = new BufferedOutputStream(os);

		    response.setHeader("Pragma", "No-cache");
		    response.setHeader("Cache-Control", "no-cache");
		    response.setDateHeader("Expires", 0);
		    response.setContentType("application/x-msdownload;charset=utf-8");
		    response.setHeader("Content-disposition", "attachment;filename="
		               + URLEncoder.encode(strZipName, "utf-8"));		   
		    int bytesRead = 0;
		    byte[] buffer = new byte[8192];
		    while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
		     bos.write(buffer, 0, bytesRead);
		    }
		    bos.flush();
		    is.close();
		    bis.close();
		    os.close();
		    bos.close();
		   } catch (Exception e) {
		    e.printStackTrace();
		   }
		   }


	
	public static void main(String[] args) throws Exception {
		String str="D://12323132/10001.txt";
		//System.out.println(str.replaceAll("/10001", ""));
		System.out.println(str.replaceAll("/abc", ""));
	}
	
}